<?php
/*
Plugin Name: 万戈牌工具条
Plugin URI: http://wange.im/wg-toolbar.html
Description: 自从你开启此插件起，就表示默认<a href="http://wange.im" title="Life Studio" target="_blank">万戈</a>是全银河系最帅的好淫（或者是最好的帅锅），一切反对无效！ ╮(╯3╰)╭
Version: 高潮
Author: 万戈
Author URI: http://wange.im
License: GPL
*/

$pluginName = '万戈牌工具条';
$shortName = 'tb';

$position = array('top'=>'顶部','bottom'=>'底部');

$notice_num = array('2','3','4','5','6','7','8','9','10');
$num_tmp = array_unshift($notice_num, '请选择显示数量：');

$notice_interval = array('3 秒','4 秒','5 秒','6 秒','7 秒','8 秒','9 秒','10 秒');
$interval_tmp = array_unshift($notice_interval, '请选择轮播间隔：');

$sns_type = array('tb_sina'=>'新浪微博','tb_sohu'=>'搜狐微博','tb_net163'=>'网易微博','tb_baidu'=>'百度说吧','tb_twitter'=>'Twitter','tb_tencent'=>'QQ 微博','tb_digu'=>'嘀咕','tb_douban'=>'豆瓣','tb_facebook'=>'Facebook');
$type_tmp = array_unshift($sns_type,'不显示');

$options = array (
	//显示位置
	array('name' => '工具条显示位置','id' => $shortName.'_position','std' => '','type' => 'select','options' => array_values($position)),
	
	//滚动间隔
	array('name' => '公告轮播间隔','id' => $shortName.'_noticeInterval','std' => '5 秒','type' => 'select','options' => $notice_interval),
	
	//RSS 公告设置
	array('name' => 'RSS 公告地址','id' => $shortName.'_feedUrl','std' => '','type' => 'text'),
	array('name' => 'RSS 滚动显示数量','id' => $shortName.'_noticeNum','std' => '3','type' => 'select','options' => $notice_num),
	
	//开启自定义公告
	array('name' => '开启自定义公告','id' => $shortName.'_diyOnOff','std' => false,'type' => 'checkbox'),
	
	//自定义公告
	array('name' => '自定义公告一','id' => $shortName.'_diyNotice1','std' => '','type' => 'text'),
	array('name' => '自定义公告二','id' => $shortName.'_diyNotice2','std' => '','type' => 'text'),
	array('name' => '自定义公告三','id' => $shortName.'_diyNotice3','std' => '','type' => 'text'),
	array('name' => '自定义公告四','id' => $shortName.'_diyNotice4','std' => '','type' => 'text'),
	array('name' => '自定义公告五','id' => $shortName.'_diyNotice5','std' => '','type' => 'text'),
	
	//自定义 SNS 地址
	array('name' => '1.1 请选择 SNS 类型','id' => $shortName.'_snsType1','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '1.2 请输入 SNS 地址','id' => $shortName.'_snsUrl1','std' => '','type' => 'text'),
	array('name' => '2.1 请选择 SNS 类型','id' => $shortName.'_snsType2','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '2.2 请输入 SNS 地址','id' => $shortName.'_snsUrl2','std' => '','type' => 'text'),
	array('name' => '3.1 请选择 SNS 类型','id' => $shortName.'_snsType3','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '3.2 请输入 SNS 地址','id' => $shortName.'_snsUrl3','std' => '','type' => 'text'),
	array('name' => '4.1 请选择 SNS 类型','id' => $shortName.'_snsType4','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '4.2 请输入 SNS 地址','id' => $shortName.'_snsUrl4','std' => '','type' => 'text'),
	array('name' => '5.1 请选择 SNS 类型','id' => $shortName.'_snsType5','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '5.2 请输入 SNS 地址','id' => $shortName.'_snsUrl5','std' => '','type' => 'text'),
	array('name' => '6.1 请选择 SNS 类型','id' => $shortName.'_snsType6','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '6.2 请输入 SNS 地址','id' => $shortName.'_snsUrl6','std' => '','type' => 'text'),
	array('name' => '7.1 请选择 SNS 类型','id' => $shortName.'_snsType7','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '7.2 请输入 SNS 地址','id' => $shortName.'_snsUrl7','std' => '','type' => 'text'),
	array('name' => '8.1 请选择 SNS 类型','id' => $shortName.'_snsType8','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '8.2 请输入 SNS 地址','id' => $shortName.'_snsUrl8','std' => '','type' => 'text'),
	array('name' => '9.1 请选择 SNS 类型','id' => $shortName.'_snsType9','std' => '不显示','type' => 'select','options' => array_values($sns_type)),
	array('name' => '9.2 请输入 SNS 地址','id' => $shortName.'_snsUrl9','std' => '','type' => 'text')
);

function wgToolBar_add_admin() {
	global $pluginName, $shortName, $options;
	if ( $_GET['page'] == basename(__FILE__) ) {
		if ( 'save' == $_REQUEST['action'] ) {
			foreach ($options as $value) {
				update_option( $value['id'], $_REQUEST[ $value['id'] ] );
			}
			foreach ($options as $value) {
				if( isset( $_REQUEST[ $value['id'] ] ) ) {
					update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); 
				} else {
					delete_option( $value['id'] );
				}
			}
			header("Location: themes.php?page=wg-toolbar.php&saved=true");
			die;
		} elseif( 'reset' == $_REQUEST['action'] ) {
			foreach ($options as $value) {
				delete_option( $value['id'] ); 
				update_option( $value['id'], $value['std'] );
			}
			header("Location: themes.php?page=wg-toolbar.php&reset=true");
			die;
		}
	}
	add_theme_page($pluginName, $pluginName, 'edit_themes', basename(__FILE__), 'wgToolBar_admin');
}
function wgToolBar_admin() {
	global $pluginName, $shortName, $options;
	if ( $_REQUEST['saved'] ) echo '<div class="updated"><p><strong>设置已保存</strong></p></div>';
	if ( $_REQUEST['reset'] ) echo '<div class="updated"><p><strong>设置已重置</strong></p></div>';
?>
	<style type="text/css">
		.form-table th{vertical-align:middle;}
	</style>
	<div class="wrap">
		<?php screen_icon(); ?>
		<h2>万戈牌工具条设置</h2>
		<form method="post">
			<table class="form-table" >
				<?php foreach ($options as $value) { 
					if ($value['type'] == "text") { ?>
						<tr> 
							<th><strong><?php echo $value['name']; ?>:</strong></th>
							<td>
								<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" size="40" />
							</td>
						</tr>
					<?php } elseif ($value['type'] == "checkbox") { ?>
						<tr> 
							<th><strong><?php echo $value['name']; ?>:</strong></th>
							<td>
								<input style="width: auto;" name="<?php echo $value['id']; ?>" type="checkbox" <?php if(get_settings($value['id']) ) { echo ' checked="checked"'; }?>/>
							</td>
						</tr>
					<?php } elseif ($value['type'] == "select") { ?>
						<tr> 
							<th><strong><?php echo $value['name']; ?>:</strong></th>
							<td>
								<select style="font-size:12px" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
								<?php foreach ($value['options'] as $option) { ?>
								<option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; }?>><?php echo $option; ?></option>
								<?php } ?>
								</select>
							</td>
						</tr>
					<?php } ?>
					<?php 
				}
				?>
			</table>
			<div class="submit">
				<input class="button-primary" name="save" type="submit" value="保存设置" />    
				<input type="hidden" name="action" value="save" />
			</div>
		</form>
		<form method="post" class="defaultbutton">
			<div class="submit">
				<input class="button-secondary" name="reset" type="submit" value="重置设置" />
				<input type="hidden" name="action" value="reset" />
			</div>
		</form>
	</div>
	<?php
}
add_action('admin_menu', 'wgToolBar_add_admin');

//滚动公告
function wgNotice() {
	if( get_option('tb_diyOnOff') ) {
		for( $i=1; $i<6; $i++ ){
			if( get_option('tb_diyNotice'.$i) !== '' ){
				$wgNotice .= '<li>'.trim( get_option('tb_diyNotice'.$i) ).'</li>';
			}
		}
	} else {
		date_default_timezone_set('PRC');
		include_once(ABSPATH . WPINC . '/feed.php');
		if( trim( get_option('tb_feedUrl') ) !== '' ){
			$feedurl = trim( get_option('tb_feedUrl') );
		} else {
			$feedurl = get_option('siteurl').'/feed';
		}
		$feed = fetch_feed( $feedurl );
		if (!is_wp_error( $feed ) ) {
			$maxitems = $feed->get_item_quantity( intval( get_option('tb_noticeNum') ) ); 
			$items = $feed->get_items(0, $maxitems); 
		}
		foreach ($items as $item) {
			$wgNotice .= '<li><a title="'.strip_tags($item->get_date('j F Y | g:i a')).'" href="'.$item->get_permalink().'">'.strip_tags(addslashes($item->get_description())).'</a></li>';
		}
	}
	$before = '<ul id="wgNotice">';
	$after = '</ul>';
	$wgNotice = $before.$wgNotice.$after;
	return $wgNotice;
}


//SNS
function wgSNS(){
	global $sns_type;
	$before = '<ul id="wgSNS">';
	$after = '</ul>';
	for( $i=1; $i<=count($sns_type)-1; $i++ ){
		if( get_option('tb_snsType'.$i) != '不显示' ) {
			$snsList .= '<li><a id="'.array_search( get_option('tb_snsType'.$i), $sns_type ).'" title="'.get_option('tb_snsType'.$i).'" target="_blank" href="'.get_option('tb_snsUrl'.$i).'">'.get_option('tb_snsType'.$i).'</a></li>';
		}
	}
	$addFav = '<li><a id="tb_fav" title="加入收藏" href="javascript:void(0)" onclick="addFavorite()">加入收藏</a></li>';
	$mailMe = '<li><a id="tb_mail" title="Email" href="mailto:'.get_option('admin_email').'">Email</a></li>';
	$siteFeed = '<li><a id="tb_rss" title="订阅本站" target="_blank" href="'.get_option('siteurl').'/feed">订阅本站</a></li>';
	$wgSNS = $before.$snsList.$wgSNS.$addFav.$mailMe.$siteFeed.$after;
	return $wgSNS;
}

//滚动公告 + SNS
$wgToolBar = '<div id="wgToolBar">'.wgNotice().wgSNS().'</div>';

//滚动间隔
switch( get_option('tb_noticeInterval') ){
	case '3 秒':$interval_seconds = 3000;break;
	case '4 秒':$interval_seconds = 4000;break;
	case '5 秒':$interval_seconds = 5000;break;
	case '6 秒':$interval_seconds = 6000;break;
	case '7 秒':$interval_seconds = 7000;break;
	case '8 秒':$interval_seconds = 8000;break;
	case '9 秒':$interval_seconds = 9000;break;
	case '10 秒':$interval_seconds = 10000;break;
}

//过滤换行
function DeleteHtml($str){
	$str = trim($str);
	$str= preg_replace("/\s+/", " ", $str);
	return trim($str);
}

//脚本和样式
add_action('wp_head', 'toolbar_js_css');
function toolbar_js_css(){
	global $wgToolBar,$position;
	//CSS
	$toolBarStyleUrl = WP_PLUGIN_URL . '/wg-toolbar/style.css';
	$toolBarFile = WP_PLUGIN_DIR . '/wg-toolbar/style.css';
	if ( file_exists($toolBarFile) ) {
		wp_register_style('toolBarStyleSheet', $toolBarStyleUrl);
		wp_print_styles( 'toolBarStyleSheet');
	}
	//jQuery
	if (!is_admin()) {
		wp_deregister_script( 'jquery' );
		wp_register_script('jquery', plugins_url('/js/jquery.js', __FILE__), '', '1.3.1', 0 );
		wp_print_scripts( 'jquery' );
	}
?>
	<script type="text/javascript">
		jQuery(document).ready(function($){
			$('body').append('<?php echo DeleteHtml( $wgToolBar );?>');
			$('#wgNotice').css('width',$(window).width() - $('#wgSNS').outerWidth() -40);
			$("#wgNotice li:gt(0)").css("display","none");var B=$("#wgNotice li:last");var C=$("#wgNotice li:first");setInterval(function(){if(B.is(":visible")){C.fadeIn(500).addClass("in");B.hide()}else{$("#wgNotice li:visible").addClass("in");$("#wgNotice li.in").next().fadeIn(500);$("#wgNotice li.in").hide().removeClass("in")}},<?php global $interval_seconds;echo $interval_seconds;?>);
		});
		function addFavorite(){if(document.all){try{window.external.addFavorite(window.location.href,document.title)}catch(e){alert("加入收藏失败，请使用 Ctrl+D 进行添加")}}else if(window.sidebar){window.sidebar.addPanel(document.title,window.location.href,"")}else{alert("加入收藏失败，请使用 Ctrl+D 进行添加")}}
	</script>
	<style type="text/css">
		<?php 
		if( array_search( get_option('tb_position'), $position ) == 'top' ){
			$style = 'body{margin-top:30px;}';
			$style .= '#wgToolBar{border-radius:0 0 5px 5px;box-shadow:0px 5px 5px #565656;-moz-border-radius:0 0 5px 5px;-webkit-border-radius:0 0 5px 5px;-khtml-border-radius:0 0 5px 5px;-webkit-box-shadow:0px 5px 5px #565656;-moz-box-shadow:0px 5px 5px #565656;top:0;_position:absolute;_top:expression(0+((e=document.documentElement.scrollTop)?e:document.body.scrollTop)+"px");}';
		} else {
			$style = 'body{margin-bottom:30px;}';
			$style .= '#wgToolBar{border-radius:5px 5px 0 0;box-shadow:0px -5px 5px #565656;-moz-border-radius:5px 5px 0 0;-webkit-border-radius:5px 5px 0 0;-khtml-border-radius:5px 5px 0 0;-webkit-box-shadow:0px -5px 5px #565656;-moz-box-shadow:0px -5px 5px #565656;bottom:0;_position:absolute;_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,0)||0)-(parseInt(this.currentStyle.marginBottom,0)||0)));}';
		}
		echo $style;
		?>
	</style>
<?php }
?>